﻿using OrderStateMachineWebApi_V2.Services;
using OrderStateMachineWebApi_V2.ServicesImpl;

namespace OrderStateMachineWebApi_V2.Models
{
    public class Order
    {
        public string Id { get; }
        public IOrderState State { get; set; }
        public DateTime CreatedAt { get; } = DateTime.UtcNow;
        public DateTime? LastUpdated { get; private set; }
        public List<OrderTransition> History { get; } = new();

        public Order(string id)
        {
            Id = id;
            State = new NewState(this);
            RecordTransition("Created", OrderAction.Create);
        }

        public void RecordTransition(string fromState, OrderAction action)
        {
            History.Add(new OrderTransition(
                FromState: fromState,
                ToState: State.StateName,
                Action: action,
                Timestamp: DateTime.UtcNow
            ));

            LastUpdated = DateTime.UtcNow;
        }
    }

    public record OrderTransition(
        string FromState,
        string ToState,
        OrderAction Action,
        DateTime Timestamp
    );

    public enum OrderAction
    {
        Create,     // 创建订单
        Submit,     // 提交订单
        Process,    // 开始处理
        Ship,       // 发货
        Deliver,    // 交付
        Complete,   // 完成
        Cancel,     // 取消
        Hold,       // 暂停
        Resume,     // 恢复
        Return      // 退货
    }
}
